<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 用户订单
 */
namespace app\agent\controller\system;
use think\Db;
class UserQOrder extends \app\agent\controller\Common
{
    public function index(){
        $batch = (new \app\common\model\UserOrders())->where(['agent_id' => $this->userInfo['id']])->group('batch')->order('id desc')->select();
        $this->assign('batch',$batch);
        $driver = (new \app\common\model\Driver())->where(['agent_id' => $this->userInfo['id']])->order('id desc')->select();
        $this->assign('driver',$driver);
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
       
        $query = (new \app\common\model\DriverQOrder())->where(['agent_id' => $this->userInfo['id']]);
        isset($search['goods_title']) && $search['goods_title'] != "" ? $query->where("title like '%{$search['goods_title']}%'") : '';
        isset($search['batch']) && $search['batch'] != "" ? $query->where("batch like '%{$search['batch']}%'") : '';
      
        $query->order("id desc");

        $list = $query->limit($page, $limit)->append([
            'addtime_text','state_text','driver_title','driver_title'
            ])->select();
        $count = $query->count();
        return self::successResult('',$list,$count);
    }

   public function info(){
        $id = request()->param('id');
        $order = (new \app\common\model\DriverQOrder())->where(['id' => $id])->find();
        $info = (new \app\common\model\UserOrders())
                    ->alias("order")
                    ->join("{$this->tdb}user_orders_goods order_goods","order_goods.order_id = order.id")
                    ->where(['order.q_order_id' => $order['id']])
                    ->append([
                        'addtime_text','state_text','driver_q_title','driver_title',
                        'driver_q_state_text','driver_state_text','bus_state_text','goods_info'
                        ])
                    ->select();

        $this->assign("info",$info);
        $this->assign("order",$order);
        return view();
   }
   public function add(){
     
        $id = request()->param('id');
        $order = (new \app\common\model\DriverQOrder())->where([
            'id' => $id
        ])->find();
        // if($order['state'] == 2){
        //     return self::errorResult("该笔订单已经处理完成！！！");
        // }
        Db::startTrans();
        try{
            $message = "订单创建失败,请重试";
            $process = true;

            $row = (new \app\common\model\UserOrders())->where(['q_order_id' => $id])->update([
                'driver_state' => 1,
                'driver_q_state' => 2,
            ]);
            if(!$row){
                $message = "修改订单状态失败,请重试";
                $process = false;
            }
            $row = (new \app\common\model\DriverQOrder())->where(['id' => $id])->update([
                'state' => 2,
            ]);
            if(!$row){
                $message = "修改订单状态失败,请重试";
                $process = false;
            }
            if ($process !== false) {
                Db::commit(); // 提交事务
            } else {
                Db::rollback(); // 回滚事务
            }
        } catch (\Exception $e) {
            $process = false;
            $message = $e->getMessage();
            Db::rollback(); // 回滚事务
        }
        if ($process !== false) {
            return self::successResult("成功");
        } else {
            return self::errorResult($message);
        }

    }
  
  
 
}
